//
//  HCNvaDetailViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/5/24.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCNvaDetailViewController.h"

#import "HCNavContentViewController.h"

#import "ZJScrollPageView.h"

#import "HCFindNavModel.h"

@interface HCNvaDetailViewController () {
    ZJScrollPageView *scrollPageView;
    NSMutableArray *ar;
    HCFindNavModel *hc_model_;
    HCNavContentViewController *currentCon;
}

@end

@implementation HCNvaDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textTitle = @"发现";
    
    //必要的设置, 如果没有设置可能导致内容显示不正常
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.dataRequest.delegate = self;
    
//    self.dataRequest post_HCFondProjectList:
    
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    // 缩放标题
    style.scaleTitle = YES;
    // 颜色渐变
    style.gradualChangeTitleColor = YES;
    // 设置附加按钮的背景图片
    // 设置子控制器 --- 注意子控制器需要设置title, 将用于对应的tag显示title
    NSArray *childVcs = [NSArray arrayWithArray:[self setupChildVcAndTitle]];
    // 初始化
    scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 0, self.centerView.bounds.size.width, self.centerView.bounds.size.height) segmentStyle:style childVcs:childVcs parentViewController:self];
    [self.centerView addSubview:scrollPageView];
    
    [scrollPageView setSelectedIndex:[self.dataObject integerValue] animated:YES];
    
    // Do any additional setup after loading the view.
}

-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {
    NSLog(@"数据请求成功:%@",dictionary);
    
    hc_model_ = [[HCFindNavModel alloc] initWithDictionary:dictionary error:nil];
    
    currentCon.model = hc_model_;
    
    [currentCon.tableView_.mj_header endRefreshing];
    
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    NSLog(@"数据请求失败");
    [currentCon.tableView_.mj_header endRefreshing];

}

- (NSArray *)setupChildVcAndTitle {
    
    NSArray *avb = [NSArray arrayWithArray:self.parameterObject];
     ar = [NSMutableArray array];
    
    __block __weak HCNvaDetailViewController *weakSelf = self;
    
    for (int i = 0; i<avb.count; i++) {
        HCNavContentViewController *vc1 = [HCNavContentViewController new];
        vc1.view.backgroundColor = [UIColor redColor];
        vc1.callBack = ^(NSIndexPath *indexPath) {
            HCFindNavData *data = hc_model_.project_shop[indexPath.row];
            [weakSelf pushChildrenViewController:@"HCNavDDViewController" parameterObject:data dataObject:nil];
        };
        vc1.block = ^(HCNavContentViewController *viewController){
            currentCon = nil;
            currentCon = viewController;
            [weakSelf.dataRequest cancelDownload];
            weakSelf.dataRequest.delegate = self;
            [weakSelf.dataRequest post_naviDetailWithStoreId:[NSString stringWithFormat:@"%@",avb[i][@"pid"]]];
        };
        vc1.centerView__.frame = CGRectMake(0, 0, vc1.centerView__.frame.size.width, vc1.centerView__.frame.size.height - CGRectGetHeight(scrollPageView.frame));
        vc1.centerView__.backgroundColor = [UIColor whiteColor];
        vc1.title = [NSString stringWithFormat:@"%@",avb[i][@"name"]];
        [ar addObject:vc1];
    }
    return ar;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
